package kayao.client.scene;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import kayao.common.KayaoValues;
import kayao.client.KayaoActivity;
import kayao.client.data.SharedMemory;

import com.chopsticks3d.ChopsticksView;
import com.chopsticks3d.scene.camera.Camera;

import android.os.SystemClock;
import android.util.Log;

/**
* The android view for this application and the thread
* that handles all the drawing.
*/
public class KayaoView extends ChopsticksView {
	private KayaoActivity mKayaoActivity;
	private long mLastTimeDraw;
	private long mTime;
	private long mTimeDelta;
	
	private float[] fogColor = {0.5f, 0.5f, 0.5f, 1.0f};
	private FloatBuffer fogColorBuffer;
	
	long lastFrame = 0;
	int fps = 0;
	
	private boolean initialized = false;
	
	/**
	* Constructor for setting up the rendering.
	* @param kayaoActivity the activity of the Kayao application.
	*/
	public KayaoView(KayaoActivity kayaoActivity) {
		super(kayaoActivity);
		mKayaoActivity = kayaoActivity;
		
		ByteBuffer byteBuf = ByteBuffer.allocateDirect(fogColor.length * 4);
		byteBuf.order(ByteOrder.nativeOrder());
		fogColorBuffer = byteBuf.asFloatBuffer();
		fogColorBuffer.put(fogColor);
		fogColorBuffer.position(0);
		
		lastFrame = SystemClock.uptimeMillis();
		mLastTimeDraw = SystemClock.uptimeMillis();		
	}
	
	@Override
	public void onSizeChanged(int width, int height, int oldWidth, int oldHeight) {
		super.onSizeChanged(width, height, oldWidth, oldHeight);
		
		if (!initialized) {
			// Initialize camera now that width and height of the display is known.
			Camera.setDisplayDimensions(width, height);
			SharedMemory.getInstance().setScreenWidth(width);
			
			// Initialize activity.
			mKayaoActivity.onInitialize();
			mKayaoActivity.onInitializationComplete();
			initialized = true;
		}
	}
	
	@Override
	public void onSurfaceCreated(GL10 gl, EGLConfig egl) {
		super.onSurfaceCreated(gl, egl);
		
		gl.glFogf(GL10.GL_FOG_MODE, GL10.GL_EXP);
		gl.glFogfv(GL10.GL_FOG_COLOR, fogColorBuffer);
		gl.glFogf(GL10.GL_FOG_DENSITY, 0.05f);
		gl.glHint(GL10.GL_FOG_HINT, GL10.GL_DONT_CARE);
		gl.glFogf(GL10.GL_FOG_START, 1.0f);
		gl.glFogf(GL10.GL_FOG_END, 40.0f);
		gl.glEnable(GL10.GL_FOG);
	}
	
	@Override
	public void onDrawFrame(GL10 gl) {
		// Calculation of delta time for drawing to see if thread should sleep
		mTime = SystemClock.uptimeMillis();
		mTimeDelta = mTime - mLastTimeDraw;
		if (mTimeDelta < KayaoValues.MAX_DRAW_TIME) {
			try {
				Thread.sleep(KayaoValues.MAX_DRAW_TIME - mTimeDelta);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		mLastTimeDraw = SystemClock.uptimeMillis();
		
		super.onDrawFrame(gl);
		
//		printFPS();
	}
	
	private void printFPS() {
		long now = SystemClock.uptimeMillis();
        if(now - lastFrame >= 1000l) {
            Log.d("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", fps + " fps");
            fps = 0;
            lastFrame = now;
        } else {
        	fps++;
        }
	}
	
	public void terminate() {
		this.setScene(null);
		mKayaoActivity.onLogOut();
	}
}
